package com.hjx.movie.core.system.service.commons.mail;

import com.hjx.movie.core.system.vo.MailMsgVO;
import com.hjx.movie.core.util.PasswordUtil;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;


import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>Title:</p>
 * <p>Description:</p>
 *
 * @author hjx
 * @date 2021/2/1 - 22:02
 */
@Service
@Slf4j
@Transactional
public class MailService {
    @Value("${spring.mail.username}")
    private String from;

    @Resource
    JavaMailSender mailSender;

    @Resource
    FreeMarkerConfigurer configurer;

    @Resource
    RedisTemplate<String,String> redisTemplate;

    public void sendMailCode(MailMsgVO mailMsgVO){
        log.debug("获取到的入参为:{}",mailMsgVO);
        String code = PasswordUtil.generateRandomCode(null);
        //将获取的邮箱和验证码存入缓存
        redisTemplate.opsForValue().set(getMailKey(mailMsgVO.getRecipient()),code,5, TimeUnit.MINUTES);
        log.debug("生成的验证码为:{}",code);
        if (mailMsgVO.isHtml()){
            Map<String,Object> model = new HashMap<>();
            model.put("title",mailMsgVO.getSubject());
            model.put("recipient",mailMsgVO.getRecipient());
            model.put("content","验证码为:"+code);
            mailMsgVO.setContent(getHTMLFromTemplate("mail.ftlh",model));
        }else {
            mailMsgVO.setContent("验证码为:"+code);
        }
        sendMessageMail(mailMsgVO);
    }

    public static String getMailKey(String username){
        return "MailCacheCode:"+username;
    }

    public String getHTMLFromTemplate(String templateName,Map<String,Object> model){
        try {
            Template template = configurer.getConfiguration().getTemplate(templateName);
            return FreeMarkerTemplateUtils.processTemplateIntoString(template,model);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


    public void sendMessageMail(MailMsgVO mailMsg){
        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(from);
            helper.setTo(mailMsg.getRecipient());
            helper.setSubject(mailMsg.getSubject());
            helper.setText(mailMsg.getContent(),mailMsg.isHtml());
            mailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

}


